package com.xyzq.webapp.action.ya.data;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzq.model.common.DataDictionary;
import com.xyzq.model.ya.data.Staff;
import com.xyzq.service.common.DataDictionaryManager;
import com.xyzq.service.ya.StaffManager;
import com.xyzq.webapp.action.BaseAction;

public class StaffAction extends BaseAction implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 872759232204735724L;
	private Long id;
	private Staff staff;
	private List<Staff> staffGroup;
	private List<DataDictionary> deptGroup;
	private StaffManager staffManager;
	private DataDictionaryManager dataDictionaryManager;

	public String list() {
		staffGroup = this.staffManager.getAll();
		this.logUserAction("StaffAction", "read all list");
		return SUCCESS;
	}

	public String edit() {
		if (getId() != null) {
			staff = this.staffManager.get(getId());
			this.logUserAction("StaffAction", "ready edit:"+this.staff);
		} else {
			staff = new Staff();
			this.logUserAction("StaffAction", "ready input:"+this.staff);
		}
		return SUCCESS;
	}

	public String save() {
		if (cancel != null) {
			return CANCEL;
		}
		boolean isNew = (this.staff.getId() == null);
		if (isNew) {
			this.staff.setSubTime(new Date());
		} else {
			this.staff.setModifyTime(new Date());
			this.staff.setSubTime(this.staffManager.get(this.staff.getId())
					.getSubTime());
		}
		this.staffManager.save(staff);
		String key = (isNew) ? "staff.added" : "staff.updated";
		saveMessage(getText(key));
		if (!isNew) {
			this.logUserAction("StaffAction", "edit:"+this.staff);
			return INPUT;
		} else {
			this.logUserAction("StaffAction", "save:"+this.staff);
			return SUCCESS;
		}
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		this.deptGroup = this.dataDictionaryManager.findByModule("staffDept");
	}

	public Staff getStaff() {
		return staff;
	}

	public List<Staff> getStaffGroup() {
		return staffGroup;
	}

	public List<DataDictionary> getDeptGroup() {
		return deptGroup;
	}

	public StaffManager getStaffManager() {
		return staffManager;
	}

	public DataDictionaryManager getDataDictionaryManager() {
		return dataDictionaryManager;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}

	public void setStaffGroup(List<Staff> staffGroup) {
		this.staffGroup = staffGroup;
	}

	public void setDeptGroup(List<DataDictionary> deptGroup) {
		this.deptGroup = deptGroup;
	}

	public void setStaffManager(StaffManager staffManager) {
		this.staffManager = staffManager;
	}

	public void setDataDictionaryManager(
			DataDictionaryManager dataDictionaryManager) {
		this.dataDictionaryManager = dataDictionaryManager;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

}
